<?php

namespace app\index\controller;

use app\common\model\ArticleCategory;
use think\Controller;
use app\common\model\Site;
use app\common\model\Article;
use app\common\model\News;
use app\common\model\Base;

class Pull extends Controller
{

    public function _initialize()
    {
        parent::_initialize();
        $params = $this->request->param();

        if (!isset($params['updatekey'])) {
            dd(json_encode(array('status' => 'faild', 'msg' => '没有正确填写updatekey')));
        }
        //查询updateKey
        $otaConfig = Base::getOtaConfig();
        if ($otaConfig['updatekey'] != $params['updatekey']) {
            dd(json_encode(array('status' => 'faild', 'msg' => '非法操作')));
        }
    }

    public function syncData($limit = 30)
    {
        //同步官方分类
        $res1 = ArticleCategory::syncFromLaysns();
        //同步官方分类
        $res2 = Article::syncFromLaysns($limit);
        $msg = '';
        if ($res1['status'] == 'success') {
            $msg = $res1['msg'];
        }
        if ($res2['status'] == 'success') {
            $msg .= ';' . $res2['msg'];
        }
        dd(json_encode(array('status' => 'success', 'msg' => $msg)));

    }


    public function initMonthViewNum()
    {
        if (date('d') == '01') {
            $upDate['day_view'] = 0;
            $upDate['month_view'] = 0;
            (New Site())->where(['id' => ['gt', 0]])->update($upDate);
            dd(json_encode(array('status' => 'success', 'msg' => '成功重置全部资源的月访问量为0！')));
        }
    }

    public function imgLocalDown()
    {
        $res = Article::imgLocalDown();

        dd(json_encode($res));

    }
}
